package com.xinzhi.service.impl;

import com.xinzhi.dao.ProductMapper;
import com.xinzhi.dao.ProductTypeMapper;
import com.xinzhi.entity.Product;
import com.xinzhi.entity.ProductIntro;
import com.xinzhi.entity.ProductType;
import com.xinzhi.entity.Result;
import com.xinzhi.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductTypeMapper productTypeMapper;


    @Override
    public List<ProductType> getAllProductType() {
        return null;
    }
    @Override
    public Result getProductByColId(String colId) {
        if (colId == null || colId==""){
            return Result.error("二级分类id为空");
        }
        Integer tempColId = Integer.valueOf(colId);
        List<Product> productByColId = productMapper.getProductByColId(tempColId);
        List<ProductIntro> productIntroList = new ArrayList<>();
        for (Product product : productByColId) {
            ProductIntro productIntro = new ProductIntro();
            productIntro.setId(product.getId());
            productIntro.setProductname(product.getProductname());
            productIntro.setMainimage(product.getMainimage());
            productIntro.setPrice(product.getPrice());
            productIntroList.add(productIntro);
        }
        return Result.ok(productIntroList);
    }

    @Override
    public Result getProductBylId(String id) {
        if (id == null || id==""){
            return Result.error("商品id为空");
        }
        Integer tempId = Integer.valueOf(id);
        return Result.ok(productMapper.getProductById(tempId));
    }


}
